/*
 * @Author: tkp
 * @Date: 2024-11-20 15:24:00
 */
const state = {
    visitedViews: [],
}
const mutations = {
    ADD_VISITED_VIEW: (state, data) => {
        if (state.visitedViews.some(v => v.path === data.path)) return
        state.visitedViews.push(
            Object.assign({}, data, { title: data.meta.title || '无标题' })
        )
    },
    DEL_VISITED_VIEW: (state, data) => {
        for (const [i, v] of state.visitedViews.entries()) {
            if (v.path === data.path) {
                state.visitedViews.splice(i, 1)
                break
            }
        }
    },
    DEL_OTHER_VISITED_VIEW: (state, data) => {
        state.visitedViews = [Object.assign({}, data, { title: data.meta.title || '无标题' })]
    },
    CLEAR_VISITED_VIEWS: (state, data) => {
        state.visitedViews = []
    },
}
const actions = {
    addView({ dispatch }, data) {
        dispatch('addVisitedView', data)
    },
    addVisitedView({ commit }, data) {
        commit('ADD_VISITED_VIEW', data)
    },
    delView({ dispatch, state }, data) {
        return new Promise(resolve => {
            dispatch('delVisitedView', data)
            resolve({
                visitedViews: [...state.visitedViews]
            })
        })
    },
    delVisitedView({ commit, state }, data) {
        return new Promise(resolve => {
            commit('DEL_VISITED_VIEW', data)
            resolve([...state.visitedViews])
        })
    },
    delOtherView({ dispatch, state }, data) {
        return new Promise(resolve => {
            dispatch('delOtherVisitedView', data)
            resolve({
                visitedViews: [...state.visitedViews]
            })
        })
    },
    delOtherVisitedView({ commit, state }, data) {
        return new Promise(resolve => {
            commit('DEL_OTHER_VISITED_VIEW', data)
            resolve([...state.visitedViews])
        })
    },
    delAllView({ dispatch, state }) {
        return new Promise(resolve => {
            dispatch('delAllVisitedView')
            resolve({
                visitedViews: [...state.visitedViews]
            })
        })
    },
    delAllVisitedView({ commit, state }) {
        return new Promise(resolve => {
            commit('CLEAR_VISITED_VIEWS')
            resolve([...state.visitedViews])
        })
    },
}
export default {
    namespaced: true,
    state,
    mutations,
    actions
}